Helper.using('py.Figures', function (ns) {

    ns.Polygon = function (params) {
        ns.Figure.call(this, params);
        ns.setType('Polygon');

        var visual = new Kinetic.Polygon({
                points: [], //[{x:1,y:2},{x:3,y:4}] or [1,2,3,4]
                strokeWidth:2,
                stroke:'black',
                draggable:false
            });

        this.getVertexCount = Helper.abstractMethod();
        this.getPoints = Helper.abstractMethod();

        this.update = function (viewPort) {
            var points = this.getPoints(),
                _points = [];
            for (var i = 0; i < this.getVertexCount(); ++i) {
                _points[i] = viewPort.getPixelPoint(points[i].getPosition());
            }
            this.visual.points = _points;
        };

        this.drawFunc = function(viewPort) {
            this.update(viewPort);
            this.layer().draw();
        };

        this.visual(visual);
    };

    ns.PolygonByPoints = function(params) {
        ns.Polygon.call(this, params);
        this.setType('PolygonByPoints');

        if (!params.points || params.points.length < 3) {
            throw new Error('Wrong parameter');
        }
        var _points = params.points;

        this.setPoint = function(index, point) {
            _points[index] = point;
        };

        this.setPoints = function(points) {
            _points = points;
        };

        this.getVertexCount = function(){
            return _points.length;
        };

        this.getPoints = function (){
            return _points;
        };
    };

    ns.RegularPolygon = function(params) {
        ns.Polygon.call(this, params);
        this.setType('RegularPolygon');

        var empty = null;

        params = Helper.extend({
            vertexCount : empty,
            points : empty //Hai điểm liên tiếp nhau trong hình đa giác đều.
        }, params);

        this.formed = function () {
            return params && params.points && params.vertexCount && params.points.length && params.points.length == 2;
        };

        var _vertexCount = params.vertexCount,
            _point = [];
        _point[0] = params.points[0];
        _point[1] = params.points[1];

        var polygonPoints = ns.getVerteciesOfRegularPolygon(_point[0], _point[1], _vertexCount);

        this.setVertexCount = function(num) {
            _vertexCount = num;
            polygonPoints = ns.getVerteciesOfRegularPolygon(_point[0], _point[1], _vertexCount);
        };

        this.setPoint = function(index, point) {
            if (index < 2) {
                _point[index] = point;
            }
            polygonPoints = ns.getVerteciesOfRegularPolygon(_point[0], _point[1], _vertexCount);
        };

        //parameter: points: array of 2 Points
        this.setPoints = function(points) {
            _point = points;
            polygonPoints = ns.getVerteciesOfRegularPolygon(_point[0], _point[1], _vertexCount);
        };

        this.getPoints = function (){
            return polygonPoints;
        };

        this.getVertexCount = function(){
            return _vertexCount;
        };

    };

});